package com.jinken.service.impl;

import com.jinken.common.result.BaseResult;
import com.jinken.common.service.IOrderService;
import com.jinken.common.service.IStockService;
import com.jinken.request.BusinessRequest;
import com.jinken.service.IBusinessService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

@Service
public class BusinessServiceImpl implements IBusinessService {

    /**
     * 库存服务
     */
    @DubboReference
    private IStockService stockService;
    /**
     * 订单服务
     */
    @DubboReference
    private IOrderService orderService;

    /**
     * 全局事务注解
     * @GlobalTransactional
     */
    @GlobalTransactional
    public BaseResult handlerBusiness(BusinessRequest request) {
        //扣减库存
        BaseResult stockResult = stockService.deduct(request.getCommodityCode(), request.getCount());
        //下单
        BaseResult orderResult = orderService.createOrder(request.getUserId(),
                request.getCommodityCode(),
                request.getCount(),
                request.getMoney());
        if (stockResult.getCode() == 200 && orderResult.getCode() == 200) {
            return BaseResult.success();
        }
        return BaseResult.error("下单失败");
    }
}
